package com.szcinda.service.dto.transportType;

import com.szcinda.repository.TransportType;
import com.szcinda.repository.TransportTypeRepository;
import com.szcinda.repository.User;
import com.szcinda.repository.UserRepository;
import com.szcinda.service.SnowFlakeFactory;
import com.szcinda.service.dto.log.LogCreateDto;
import com.szcinda.service.dto.log.LogService;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class TransportTypeServiceImpl implements TransportTypeService {
    private final SnowFlakeFactory snowFlakeFactory;
    private final TransportTypeRepository transportTypeRepository;
    private final UserRepository userRepository;
    private final LogService logService;

    public TransportTypeServiceImpl(TransportTypeRepository transportTypeRepository, UserRepository userRepository, LogService logService) {
        this.transportTypeRepository = transportTypeRepository;
        this.userRepository = userRepository;
        this.logService = logService;
        this.snowFlakeFactory = SnowFlakeFactory.getInstance();
    }

    @Override
    public void add(TransportTypeCreateDto createDto) {
        User user = userRepository.findById(createDto.getOpId());
        TransportType transportType = transportTypeRepository.findByName(createDto.getName());
        Assert.isTrue(transportType == null, "存在相同编码的运输类型");
        TransportType type = new TransportType();
        type.setName(createDto.getName());
        type.setRemark(createDto.getRemark());
        type.setId(snowFlakeFactory.nextId("TT"));
        transportTypeRepository.save(type);
        LogCreateDto logCreateDto = new LogCreateDto(user.getId(), user.getNickName(), "新增运输类型", createDto.getName());
        logService.create(logCreateDto);
    }

    @Override
    public void delete(String id, String userId) {
        User user = userRepository.findById(userId);
        TransportType transportType = transportTypeRepository.findById(id);
        transportTypeRepository.delete(transportType);
        LogCreateDto logCreateDto = new LogCreateDto(user.getId(), user.getNickName(), "删除运输类型", transportType.getName());
        logService.create(logCreateDto);
    }

    @Override
    public List<TransportType> findAll(TransportTypeQueryDto params) {
        Specification<TransportType> specification = ((root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (!StringUtils.isEmpty(params.getName())) {
                Predicate name = criteriaBuilder.like(root.get("name").as(String.class), params.getName().trim() + "%");
                predicates.add(name);
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        });
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        return transportTypeRepository.findAll(specification, sort);
    }

    @Override
    public List<TransportType> getAll() {
        return transportTypeRepository.findAll();
    }
}
